#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/array.hpp>
#include <skyroad/log/logger.hpp>
#include <skyroad/message/message.hpp>
#include <skyroad/protocol/protocol.hpp>
#include <skyroad/protocol/protocol_io.hpp>
#include <skyroad/boost/container_device.hpp>
#include "connection.hpp"
#include "connection_manager.hpp"
#include "header.hpp"
#include "request.hpp"
#include "reply.hpp"
#include "reply_dispatcher.hpp"


namespace io = boost::iostreams;

reply_dispatcher::reply_dispatcher(
	connection_manager& connection_manager) :
	connection_manager_(connection_manager) {

}

void reply_dispatcher::dispatch(message& msg) {
	int conn_id = msg.destination()[2];
	connection_ptr conn = connection_manager_.get(conn_id);
	if (conn != NULL) {
		reply rep;
		rep.status = reply::ok;
		rep.vt_hkey_value.push_back(make_header("Connection", "Close"));
		rep.vt_hkey_value.push_back(make_header("Pragma", "No-cache"));
		rep.vt_hkey_value.push_back(make_header("Cache-Control", "no-cache"));
		rep.vt_hkey_value.push_back(make_header("Content-Type", "image/png"));

		rep.content.swap(msg.data());
		std::string::iterator p = rep.content.begin();
		rep.vt_hkey_value.push_back(make_header("Content-Length", boost::lexical_cast<std::string>(rep.content.size())));
		conn->async_write(rep);
	}					
}




